{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6a301589",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "* 2023-11-6\n",
    "* 课程：python语言\n",
    "* week09课堂\n",
    "\n",
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9513adf",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "购物车程序项目：\n",
    "\n",
    "> 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    ">> 1.列表数据（第一种：我们自己创建的；第二种：空列表，后面补充数据）\n",
    "> 2. 让用户输入商品编号进行商品的购买。\n",
    ">> 1. ** <font style = \"color:red\">用户输入input的结果必然是字符串 str</font>\n",
    "> 3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "> 4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cc5e3479",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：20\n",
      "----小邓茶饮店（输入 quit 退出购买）----\n",
      "商品编号:0 ,商品名称: 珍珠奶茶,价格为: 6元\n",
      "商品编号:1 ,商品名称: 柠檬水,价格为: 4元\n",
      "商品编号:2 ,商品名称: 草莓奶盖,价格为: 10元\n",
      "商品编号:3 ,商品名称: 蜜桃四季春,价格为: 8元\n",
      "商品编号:4 ,商品名称: 生椰拿铁,价格为: 12元\n",
      "用户选择的商品编号：1\n",
      "您的余额为 16 元\n",
      "----小邓茶饮店（输入 quit 退出购买）----\n",
      "商品编号:0 ,商品名称: 珍珠奶茶,价格为: 6元\n",
      "商品编号:1 ,商品名称: 柠檬水,价格为: 4元\n",
      "商品编号:2 ,商品名称: 草莓奶盖,价格为: 10元\n",
      "商品编号:3 ,商品名称: 蜜桃四季春,价格为: 8元\n",
      "商品编号:4 ,商品名称: 生椰拿铁,价格为: 12元\n",
      "用户选择的商品编号：quit\n",
      "感谢光顾小店，欢迎下次光临~\n",
      "购物车： [('柠檬水', 4)]\n"
     ]
    }
   ],
   "source": [
    "alipay = int(input(\"请输入余额：\")) #用户输入\n",
    "商品列表 = [\n",
    "    (\"珍珠奶茶\",6),\n",
    "    (\"柠檬水\",4),\n",
    "    (\"草莓奶盖\",10),\n",
    "    (\"蜜桃四季春\",8),\n",
    "    (\"生椰拿铁\",12)\n",
    "\n",
    "]\n",
    "\n",
    "shopping_car = [\n",
    "    \n",
    "]\n",
    "# 3.循环执行\n",
    "while True:\n",
    " #print(商品列表)\n",
    "        print(\"----小邓茶饮店（输入 quit 退出购买）----\")\n",
    "        for i in 商品列表:\n",
    "            print(\"商品编号:%s ,商品名称: %s,价格为: %s元\"%(商品列表.index(i),i[0],i[1]))\n",
    "        # index(i)是商品列表，i[0]商品名称 i[1]商品价格 i是item\n",
    "\n",
    "        #2\n",
    "        用户选择 = (input(\"用户选择的商品编号：\"))\n",
    "        # 1. 如果输入商品编号\n",
    "        if 用户选择.isdigit(): #isdigit是数的意思 \n",
    "            用户选择 = int(用户选择)\n",
    "        #增加进购物车，扣钱\n",
    "            if alipay >  商品列表[用户选择][1]:\n",
    "                #成功加入购物车！扣完余额\n",
    "                alipay = alipay - 商品列表[用户选择][1]\n",
    "                shopping_car.append(商品列表[用户选择])\n",
    "                print(\"您的余额为 %s 元\"%(alipay))\n",
    "            else:\n",
    "                print(\"就 %s 元，还想买 %s !\"%(alipay,商品列表[用户选择][0]))\n",
    "    # 2. 如果输入“quit” 则退出\n",
    "        elif 用户选择 == \"quit\":\n",
    "            print(\"感谢光顾小店，欢迎下次光临~\")\n",
    "            break\n",
    "       # 3. 请输入正确的商品编号或退出\n",
    "        else:\n",
    "            print(\"请输入正确的商品编号或者“quit”退出小店\")\n",
    "\n",
    "print(\"购物车：\",shopping_car)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "404f27e2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22216956",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "739d65cc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63a5d90e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9e564ff1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环列表：可以遍历列表中的每一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "aa498be3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['珍珠奶茶', 6]\n",
      "['柠檬水', 4]\n",
      "['草莓奶盖', 10]\n",
      "['蜜桃四季春', 8]\n",
      "['生椰拿铁', 12]\n"
     ]
    }
   ],
   "source": [
    "for item in 商品列表:\n",
    "    print(item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88ecac63",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
